<!-- 编辑host -->
<template>
  <el-dialog title="编辑HOST" v-model="visible" width="30%" :close-on-click-modal="false" @close="handleClose">

    <el-form :model="form" :rules="rules" ref="form" label-width="80px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="ip:port" prop="ip">
        <el-input v-model="form.ip" style="width: 70%;"></el-input>
        <el-input v-model="form.port" style="width: 30%;" type="number"></el-input>
      </el-form-item>
      <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="form.password" type="password"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remarks">
        <el-input v-model="form.remarks"></el-input>
      </el-form-item>
    </el-form>

    <template #footer>
      <span class="dialog-footer">
        <el-button size="small" @click="visible = false">取 消</el-button>
        <el-button size="small" type="primary" @click="abstractHandleConfirm" :disabled="!hasFormChange"  :loading="submitLoading">确 定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import {ElFormExt} from 'components-extent';
import HostApi from '@/api/host.api';
export default {
  mixins: [ElFormExt],
  data () {
    let self = this;
    return {
      api: HostApi,
      visible: false,
      mode: 'add',
      src: null,
      rules: {
        name: { required: true, message: '请输入名称', trigger: 'blur'},
        ip: { required: true, message: '请输入IP和端口', trigger: 'blur'},
        username: { required: true, message: '请输入用户名', trigger: 'blur'},
        password: {trigger: 'blur', validator(rule, value, callback){
          if(self.mode == 'add' && !value){
            callback(new Error('请输入登录密码'));
          } else {
            callback();
          }
        }},
      }
    };
  },
  methods: {
    show(data){
      this.visible = true;
      if(data){
        this.mode = 'edit';
      } else {
        this.mode = 'add';
      }
      this.setFormData(data, function(form){
        if(!form.port){
          form.username = 'root';
          form.port = 22;
        }
      });
    },
    handleClose () {
      this.$emit('close');
    }
  }
};
</script>

<style scoped>
</style>
